import simplejson, urllib
import simplejson as json

f = open("clueweb12/washingtondc-dc.json").read()
teste = simplejson.loads(f)

if __name__ == '__main__':
	var_file = open("clueweb12/fim/washingtondc-dc.json","w")
	for i in range(1353):
 		request = simplejson.dumps(teste["x"][i]["url"])
   	 	parts = request.split("/")
		meio = parts[2]
    		url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + meio + '&sensor=false&key=AIzaSyC2A7x8T9MKxDN2c_YSw_5mzYXPhBXpqnE'
    		result = simplejson.load(urllib.urlopen(url))
		testando = result["status"]
		if (testando == 'OK'):
			conteudo_texto = var_file.write(simplejson.dumps({
  	    		'docid': teste["x"][i]["docid"], 
 	   		'url': teste["x"][i]["url"],
  	    		'timestamp': teste["x"][i]["timestamp"],
	    		'metadata': result["results"][0],
	    		'description': [
				teste["x"][i]["description"]
				]
				}, indent=4, separators=(',', ': ')))
		conteudo_texto = var_file.write(simplejson.dumps({
  	    	'docid': teste["x"][i]["docid"], 
 	   	'url': teste["x"][i]["url"],
  	    	'timestamp': teste["x"][i]["timestamp"],
	    	'description': [
			teste["x"][i]["description"]
			]
			}, indent=4, separators=(',', ': ')))
	var_file.close()
